#===============================================================================
# Koro Business Environment
# Copyright (C) 2008  Antonio Corroppoli
#
#This file is part of Koro BE.
# 
# Koro BE is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# Koro BE is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with Koro BE; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#===============================================================================

'''
Created on 12/ago/2011

@author: antstud
'''

from koro.db import database
from koro.core.context import Context
from koro.modules.commerciale import model
#from koro.modules.prodotti.model import Prodotto,PrezzoProdotto

#===============================================================================
# CodiciIvaContext
#===============================================================================
class CodiciIvaContext(Context):
    def __init__(self):
        Context.__init__(self,model.Iva)
    
#    def new_object(self):
#        new_object=self._context()
#        new_object.codice='None'
#        database.get_instance().save(new_object)
#        return new_object
    
    def _configure_new_object(self,obj):
        obj.codice='None'
#
#    def delete_object(self,obj):
#        database.get_instance().delete(obj)
#    
#    def autodelete_object(self,obj):
#        del obj


#===============================================================================
# ListiniContext
#===============================================================================
class ListiniContext(Context):
        
    def __init__(self):
        Context.__init__(self,model.Listino)
    
#    def get_objects(self,*args,**kwargs):
#        '''Restituisce tutti i listini che soddisfano i criteri, tranne il listino fornitore.'''
#        query=database.get_instance().query(self._context).\
#        order_by(model.Listino.listino_fornitore.desc(),model.Listino.ID).filter_by(listino_fornitore=False)
#        if len(kwargs)>0:
#            query=query.filter_by(**kwargs)
#        if len(args)>0:
#            for a in args:
#                query=query.filter(a)
#        return query.all()
    
    def _configure_query(self,query):
        '''Restituisce tutti i listini che soddisfano i criteri, tranne il listino fornitore.'''
        return query.order_by(model.Listino.listino_fornitore.desc(),model.Listino.ID).filter_by(listino_fornitore=False)
    
    def get_listini(self):
        '''Restituisce tutti i listini.'''
        query=database.get_instance().query(self._context).\
        order_by(model.Listino.listino_fornitore.desc(),model.Listino.ID)
        return query.all()
    
#    def save_object(self,obj):
##        obj.validate()
##        obj.pending=False
#        database.get_instance().validate_and_save(obj)
    
    def _aggiorna_prezzi_prodotto(self,listino):
        from koro.modules.prodotti.model import Prodotto,PrezzoProdotto
        db_instance=database.get_instance()
        prodotti=db_instance.query(Prodotto).all()
        for prodotto in prodotti:
            prezzo=PrezzoProdotto(prodotto,listino)
            prezzo.aggiorna_da_listino()
            db_instance.add(prezzo)
        database.get_instance().commit()
    
    def new_object(self):
        new_object=self._context()
        database.get_instance().save(new_object)
        self._aggiorna_prezzi_prodotto(new_object)
        return new_object
    
    def delete_object(self,obj):
        from koro.modules.prodotti.model import PrezzoProdotto
        database.get_instance().query(PrezzoProdotto).filter_by(fk_listino=obj.ID).delete()
        #database.get_instance().commit()
        database.get_instance().delete(obj)
    
#    def autodelete_object(self,obj):
#        del obj
    
    def listini_senza_formula(self,*args,**kwargs):#non serve
        return self.get_objects(fk_listino_formula=None)
   
